// pages/daka/daka.js
var app = getApp();
var util = require('../../utils/util.js');
var glo = require('../../utils/global.js');
var bibleData = require('../../utils/bible.js')
var oldbook = "";
var newbook = "";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    header: '',
    nickName: '',
    hiddenmodalput: true,
    inputValue: '',
    userInfo: {}
  },
  tap_header: function (e) {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        app.getUserInfo(function (userInfo) {
          wx.uploadFile({
            url: glo.https + '/person/saveHeadImg',
            filePath: tempFilePaths[0],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            method: 'POST',
            formData: {
              openid: userInfo.wxId,
              userid: userInfo.userId,
            },
            success: function (res2) {
              if (res2.statusCode === 200) {
                var data = res2.data.replace(/"/g, "");
                //  var temp_arr = tempFilePaths[0].split('/');
                var avatarUrl = glo.https + data;
                // app.getUserInfo(function(userInfo) {
                //   userInfo.avatarUrl = avatarUrl;
                //   app.setUserInfo(userInfo);
                // })
                userInfo.avatarUrl = avatarUrl;
                app.setUserInfo(userInfo);
                that.setData({
                  header: avatarUrl
                });
                wx.showToast({
                  title: '更换头像成功'
                })
              } else {
                wx.showToast({
                  title: '图片太大请更换'
                })
              }
            },
            fail: function (err) {
              console.log('我是错误:' + err);
            }
          });

        });
        // wx.saveFile({
        //   tempFilePath: tempFilePaths[0],
        //   success: function (res) {
        //     var savedFilePath = res.savedFilePath;
        //     console.log(savedFilePath);
        //     that.setData({
        //       header: savedFilePath
        //     });
        //     //移除旧图片
        //     wx.getSavedFileList({
        //       success: function (res) {
        //         console.log(res.fileList)
        //         let list = res.fileList,
        //           path;
        //         if (list.length > 1) {
        //           if (list[0].createTime > list[1].createTime) {
        //             path = list[1].filePath;
        //           } else {
        //             path = list[0].filePath;
        //           }
        //           wx.removeSavedFile({
        //             filePath: path,
        //             success: function (res) {
        //               console.log(res)
        //             }
        //           })
        //         }
        //       }
        //     });

        //   }
        // })
      }
    });

  },
  tap_nick: function (e) {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  //取消按钮
  nickCancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  authorize: function (e) {   
    let wxInfo = e.detail.userInfo,
      that = this;
    console.log(wxInfo)
    wx.request({
      url: glo.https + '/person/updatePersonWx',
      method: 'POST',
      data: {
        avatar: wxInfo.avatarUrl,
        city: wxInfo.city,
        country: wxInfo.country,
        language: wxInfo.language,
        nickname: encodeURI(wxInfo.nickName),
        province: wxInfo.province,
        gender: wxInfo.gender,
        userid: that.data.userInfo.userId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res0) {
        console.log(res0);
        if (res0.data == true) {
          wx.setStorageSync('nickName', wxInfo.nickName);

          that.setData({
            nickName: wxInfo.nickName,
            header: wxInfo.avatarUrl
          });
          app.getUserInfo(function (userInfo) {
            userInfo.nickName = wxInfo.nickName;
            userInfo.avatarUrl = wxInfo.avatarUrl;
            app.setUserInfo(userInfo);
          })
          wx.showToast({
            title: '已同步',
            icon: 'success'
          });
          //  wx.getSavedFileList({
          //    success:function(res){
          //      var list = res.fileList;
          //      for(var i=0;i<list.length;i++){
          //        var path=list[i].filePath;
          //        wx.removeSavedFile({
          //          filePath: path
          //        })
          //      }
          //    }
          //  });
        }
      }
    })

  },
  //确认昵称
  nickConfirm: function (e) {
    let that = this,
      val = that.data.inputValue;
    if (val === '') {
      return;
    }

    app.getUserInfo(function (userInfo) {
      wx.request({
        url: glo.https + '/share/saveNickName',
        data: {
          openid: userInfo.wxId,
          userid: userInfo.userId,
          nickname: encodeURI(val)
        },
        success: function (res) {
          console.log(res)
          that.setData({
            hiddenmodalput: true,
            nickName: val,
            inputValue: ''
          });
          app.getUserInfo(function (userInfo) {
            userInfo.nickName = val;
            app.setUserInfo(userInfo);
          })
        }
      })

    });
    //  wx.setStorageSync('nickName', val);


  },
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //昵称
    // var nickName = wx.getStorageSync('nickName');
    // console.log(nickName+1111);
    // if (nickName!=null){
    //   that.setData({
    //     nickName: nickName
    //   });
    // }

    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    });
    app.getUserInfo(function (userInfo) { //改
      that.setData({
        header: userInfo.avatarUrl,
        nickName: userInfo.nickName
      });
      console.log(userInfo)

    });
    // app.getUserInfo(function (userInfo) {
    //   var header1 = userInfo.avatarUrl;
    //   if (userInfo.avatarUrl.indexOf("https") == -1) {
    //      header1 = glo.https + userInfo.avatarUrl;
    //   }
    //   that.setData({
    //     header: header1
    //   });

    // });


    //头像
    // wx.getSavedFileList({
    //   success: function (res) {
    //     if (res.fileList.length > 0) {
    //       that.setData({
    //         header: res.fileList[0].filePath
    //       });
    //     }else{
    //       app.getUserInfo(function (userInfo) {
    //         that.setData({
    //           header: userInfo.avatarUrl
    //         });

    //       });
    //     }
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角
   */
  onShareAppMessage: function () {

  }
})